/**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */
package org.sc.xl.easyexcel;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.util.List;

/**
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @author FXL
 *
 * @date 2024/10/20 10:43 
 * @since JDK 1.8
 */
public class CustomCellStyleHandler extends AbstractCellStyleStrategy {


    @Override
    protected void setHeadCellStyle(CellWriteHandlerContext context) {
        WriteCellData<?> cellData = context.getFirstCellData();
        CellStyle cellStyle = cellData.getOriginCellStyle();
        if (null == cellStyle) {
            cellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle();
        }
        XSSFCellStyle style = (XSSFCellStyle) cellStyle;
        Cell cell = context.getCell();
        Sheet sheet = cell.getSheet();
        WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
        if (0 == cell.getRowIndex()) {
            // 设置字体
            Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
            XSSFFont xssfFont = (XSSFFont) workbook.createFont();
            //设置字体大小
            xssfFont.setFontHeightInPoints((short) 16);
            //字体加粗
            xssfFont.setBold(true);
            //设置字体名字
            xssfFont.setFontName("微软雅黑");
            //左右居中
            style.setAlignment(HorizontalAlignment.CENTER);
            //垂直居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            //在样式用应用设置的字体;
            style.setFont(xssfFont);
            //设置第一列无边框
            if (0 == cell.getColumnIndex()) {
                //设置第一列列宽
                style.setBorderRight(BorderStyle.NONE);
                style.setBorderLeft(BorderStyle.NONE);
                style.setBorderTop(BorderStyle.NONE);
                style.setBorderBottom(BorderStyle.NONE);
            }
        }
        if (1 == cell.getRowIndex()) {
            // 设置字体
            Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
            Font font = workbook.createFont();
            XSSFFont xssfFont = (XSSFFont) font;
            //设置字体大小
            xssfFont.setFontHeightInPoints((short) 12);
            //字体加粗
            xssfFont.setBold(true);
            //设置字体名字
            xssfFont.setFontName("楷体");
            //设置第一行表头颜色 #653EB3  101,62,179
            XSSFColor xssfColor = new XSSFColor(new byte[]{(byte) 101, (byte) 62, (byte) 179}, null);
            xssfFont.setColor(xssfColor);
            //左右居中
            style.setAlignment(HorizontalAlignment.CENTER);
            //垂直居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);

            if (writeSheetHolder.getHead().size() - 1 == cell.getColumnIndex()) {
                //设置最后一列有边框
                style.setBorderRight(BorderStyle.MEDIUM);
                style.setRightBorderColor(xssfColor);
                style.setBorderLeft(BorderStyle.THIN);
                style.setLeftBorderColor(xssfColor);
                style.setAlignment(HorizontalAlignment.LEFT);
                //设置字体大小
                Font font1 = workbook.createFont();
                Font font2 = workbook.createFont();
                Font font3 = workbook.createFont();
                XSSFFont xssfFont1 = (XSSFFont) font1;
                XSSFFont xssfFont2 = (XSSFFont) font2;
                XSSFFont xssfFont3 = (XSSFFont) font3;
                String headText = "绿色箭头:正常指标" + (char) 10 + "红色箭头:异常指标";
                //设置富文本
                XSSFRichTextString xssfRichTextString = new XSSFRichTextString(headText);

                xssfFont1.setColor(new XSSFColor(new byte[]{(byte) 0, (byte) 255, (byte) 0}, null));//绿色
                //设置字体大小
                xssfFont1.setFontHeightInPoints((short) 8);
                //字体加粗
                xssfFont1.setBold(true);
                //设置字体名字
                xssfFont1.setFontName("楷体");
                xssfFont2.setColor(new XSSFColor(new byte[]{(byte) 255, (byte) 0, (byte) 0}, null));//红色
                //设置字体大小
                xssfFont2.setFontHeightInPoints((short) 8);
                //字体加粗
                xssfFont2.setBold(true);
                //设置字体名字
                xssfFont2.setFontName("楷体");
                xssfFont3.setColor(new XSSFColor(new byte[]{(byte) 0, (byte) 0, (byte) 0}, null));//黑色
                //设置字体大小
                xssfFont3.setFontHeightInPoints((short) 8);
                //字体加粗
                xssfFont3.setBold(true);
                //设置字体名字
                xssfFont3.setFontName("楷体");

                xssfRichTextString.applyFont(0, 5, xssfFont1);
                xssfRichTextString.applyFont(5, 10, xssfFont3);
                xssfRichTextString.applyFont(10, 15, xssfFont2);
                xssfRichTextString.applyFont(15, 19, xssfFont3);
                cell.setCellValue(xssfRichTextString);
            } else if (1 == cell.getColumnIndex()) {
                //设置第2列有边框
                style.setBorderLeft(BorderStyle.MEDIUM);
                style.setLeftBorderColor(xssfColor);
                style.setBorderBottom(BorderStyle.THIN);
                style.setBottomBorderColor(xssfColor);
            } else if (2 == cell.getColumnIndex()) {
                //设置第3列有边框
                style.setBorderLeft(BorderStyle.THIN);
                style.setLeftBorderColor(xssfColor);
                style.setBorderBottom(BorderStyle.THIN);
                style.setLeftBorderColor(xssfColor);
            } else if (3 == cell.getColumnIndex()) {
                //设置第三列宽度
                style.setBorderLeft(BorderStyle.THIN);
                style.setLeftBorderColor(xssfColor);
            } else if (writeSheetHolder.getHead().size() - 2 == cell.getColumnIndex()) {
                //设置倒数第二列左边线
                style.setBorderLeft(BorderStyle.NONE);
            } else {
                style.setBorderLeft(BorderStyle.THIN);
                style.setLeftBorderColor(xssfColor);
            }
            style.setBorderTop(BorderStyle.MEDIUM);
            style.setTopBorderColor(xssfColor);
            style.setBorderBottom(BorderStyle.THIN);
            style.setBottomBorderColor(xssfColor);
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            //在样式用应用设置的字体;
            style.setFont(font);
            //设置第一列无边框
            if (0 == cell.getColumnIndex()) {
                //设置第一列列宽
                style.setBorderRight(BorderStyle.NONE);
                style.setBorderLeft(BorderStyle.NONE);
                style.setBorderTop(BorderStyle.NONE);
                style.setBorderBottom(BorderStyle.NONE);
                style.setFillPattern(FillPatternType.NO_FILL);
                style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            } else {
                style.setFillForegroundColor(new XSSFColor(new java.awt.Color(239, 235, 247), new DefaultIndexedColorMap()));
            }
        }
        style.setWrapText(true);
        //使用新色
        cellData.setOriginCellStyle(style);
        //去掉底色
        context.getFirstCellData().setWriteCellStyle(null);
    }


    @Override
    protected void setContentCellStyle(CellWriteHandlerContext context) {
        WriteCellData<?> cellData = context.getFirstCellData();
        CellStyle cellStyle = cellData.getOriginCellStyle();
        if (null == cellStyle) {
            cellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle();
        }
        XSSFCellStyle style = (XSSFCellStyle) cellStyle;

        Cell cell = context.getCell();
        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
        WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
        // 设置字体
        Font font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 10);
        //设置字体名字
        font.setFontName("楷体");
        //左右居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置边框
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        //设置自动换行;
        style.setWrapText(true);
        //设置报表外边框 #653EB3  101,62,179
        byte[] innerSideRgb = new byte[]{(byte) 101, (byte) 62, (byte) 179};
        XSSFColor innerColor = new XSSFColor(innerSideRgb, null);
        if (1 == cell.getColumnIndex()) {
            style.setBorderLeft(BorderStyle.MEDIUM);
            style.setLeftBorderColor(innerColor);
            style.setBottomBorderColor(innerColor);
            style.setTopBorderColor(innerColor);
            style.setRightBorderColor(innerColor);
            font.setBold(true);
        } else if (2 == cell.getColumnIndex()) {
            //第二列指标名称,居左
            style.setAlignment(HorizontalAlignment.LEFT);
            style.setLeftBorderColor(innerColor);
            style.setBottomBorderColor(innerColor);
            style.setRightBorderColor(innerColor);
            style.setTopBorderColor(innerColor);
        } else if (3 == cell.getColumnIndex()) {
            //第三列项目名称
            //设置字体大小
            font.setFontHeightInPoints((short) 8);
            style.setLeftBorderColor(innerColor);
            style.setBottomBorderColor(innerColor);
            style.setTopBorderColor(innerColor);
        } else if (writeSheetHolder.getHead().size() - 1 == cell.getColumnIndex()) {
            //设置最后一列有边框
            style.setBorderRight(BorderStyle.MEDIUM);
            style.setRightBorderColor(innerColor);
            style.setBottomBorderColor(innerColor);
            style.setLeftBorderColor(innerColor);
            style.setTopBorderColor(innerColor);
        } else if (writeSheetHolder.getHead().size() - 2 == cell.getColumnIndex()) {
            //设置最后一列有边框
            style.setBorderLeft(BorderStyle.NONE);
            style.setRightBorderColor(innerColor);
            style.setBottomBorderColor(innerColor);
            style.setTopBorderColor(innerColor);
        } else {
            //设置内边框颜色
            style.setLeftBorderColor(innerColor);
            style.setTopBorderColor(innerColor);
            style.setBottomBorderColor(innerColor);
        }
        if (10 == cell.getRowIndex() || 27 == cell.getRowIndex() || 46 == cell.getRowIndex() ||
                51 == cell.getRowIndex() || 61 == cell.getRowIndex() || 66 == cell.getRowIndex() || 67 == cell.getRowIndex()
        ) {
            style.setBorderBottom(BorderStyle.MEDIUM);
        }
        // 第二页样式
        if ( 68 == cell.getRowIndex()) {
            font.setFontName("微软雅黑");
            font.setFontHeightInPoints((short) 16);
            style.setBorderLeft(BorderStyle.NONE);
            style.setBorderRight(BorderStyle.NONE);
            style.setBorderBottom(BorderStyle.MEDIUM);
        }
        if ( 69 == cell.getRowIndex()) {
            font.setFontName("楷体");
            font.setFontHeightInPoints((short) 10);
            font.setBold(true);
        }
        if(cell.getRowIndex() >= 69){
            if(cell.getColumnIndex() == 2)
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
        }

        //设置第一列无边框
        if (0 == cell.getColumnIndex()) {
            //设置第一列列宽
            style.setBorderRight(BorderStyle.NONE);
            style.setBorderLeft(BorderStyle.NONE);
            style.setBorderTop(BorderStyle.NONE);
            style.setBorderBottom(BorderStyle.NONE);
            style.setRightBorderColor(innerColor);
        }
        //在样式用应用设置的字体;
        style.setFont(font);
        if (2 == cell.getColumnIndex()) {
            setFontStyle(cell, workbook, Lists.newArrayList(7, 8, 12, 13, 14, 15, 16, 17, 18, 19, 34, 35, 36, 47, 48, 49, 50, 51));
        }
        //使用新色
        cellData.setOriginCellStyle(style);
        //去掉底色
        context.getFirstCellData().setWriteCellStyle(null);
    }

    /**
     * 2,3列模块 ()内容字体小2号
     * 带括号需要换行展示
     */
    private void setFontStyle(Cell cell, Workbook workbook, List<Integer> rowNos) {
        //设置富文本
        String cellValue = cell.getStringCellValue();
        //如果为真，则截取括号部分，换行
        int leftBracketsIndex = cellValue.indexOf("(");
        if (-1 == leftBracketsIndex) {
            return;
        }
        String firstLineText = cellValue.substring(0, leftBracketsIndex);
        String secondLineText = cellValue.substring(leftBracketsIndex);
        if (rowNos.contains(cell.getRowIndex())) {
            secondLineText = "    " + secondLineText;
            XSSFRichTextString xssfRichTextString = new XSSFRichTextString(firstLineText + "\n" + secondLineText);
            Font font1 = workbook.createFont();
            //设置字体大小
            font1.setFontHeightInPoints((short) 10);
            //设置字体名字
            font1.setFontName("楷体");
            Font font2 = workbook.createFont();
            //设置字体大小
            font2.setFontHeightInPoints((short) 8);
            //设置字体名字
            font2.setFontName("楷体");
            xssfRichTextString.applyFont(0, leftBracketsIndex, font1);
            xssfRichTextString.applyFont(leftBracketsIndex, xssfRichTextString.getString().length(), font2);
            cell.setCellValue(xssfRichTextString);
        } else {
            XSSFRichTextString xssfRichTextString = new XSSFRichTextString(firstLineText + secondLineText);
            Font font1 = workbook.createFont();
            //设置字体大小
            font1.setFontHeightInPoints((short) 10);
            //设置字体名字
            font1.setFontName("楷体");
            Font font2 = workbook.createFont();
            //设置字体大小
            font2.setFontHeightInPoints((short) 8);
            //设置字体名字
            font2.setFontName("楷体");
            xssfRichTextString.applyFont(0, leftBracketsIndex, font1);
            xssfRichTextString.applyFont(leftBracketsIndex, xssfRichTextString.getString().length(), font2);
            cell.setCellValue(xssfRichTextString);
        }
    }


}